package com.twoGroup.boot.service.imp;


import com.twoGroup.boot.mapper.AddressMapper;
import com.twoGroup.boot.pojo.Address;
import com.twoGroup.boot.service.AddressService;
import com.twoGroup.boot.utils.UserUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class AddressServiceImpl implements AddressService {
    private static Logger logger = LoggerFactory.getLogger(AddressServiceImpl.class);
    @Autowired
    private AddressMapper addressMapper;
    private UserUtils userUtils = new UserUtils();

    @Transactional
    @Override
    public int addAddressee(Integer userId, String addressee, String addresseeTelephone, String address) {
        boolean addresseeFlag = userUtils.UserStringBool(addressee, 1, 6);
        boolean addresseeTelephoneFlag = userUtils.TelephoneBool(addresseeTelephone);
        boolean addressFlag = userUtils.UserStringBool(address,5, 100);
        List<Address> addresses = addressMapper.queryAllAddressee(userId);
        try {
            if(addresseeFlag && addresseeTelephoneFlag && addressFlag){
                if(addressMapper.queryAddress(userId)<=5){
                    logger.info("添加了新的收件人");
                    return addressMapper.addAddressee(userId, addressee, addresseeTelephone, address);
                }
            }else {
                logger.info("没添加新的收件人");
                return 0;
            }
        } catch (Exception e) {
            logger.error(String.valueOf(e));
        }
        return 0;
    }

    @Override
    public int deleteAddressee(Integer addresseeId) {
        return addressMapper.deleteAddressee(addresseeId);
    }

    @Override
    public int deleteAllAddressee(Integer userId) {
        return addressMapper.deleteAllAddressee(userId);
    }

    @Transactional
    @Override
    public int updateAddressee(Integer addresseeId, String addressee, String addresseeTele, String address) {
        boolean addresseeFlag = userUtils.UserStringBool(addressee, 1, 6);
        boolean addresseeTelephoneFlag = userUtils.TelephoneBool(addresseeTele);
        boolean addressFlag = userUtils.UserStringBool(address,5, 50);
        try {
            if(addresseeFlag&&addresseeTelephoneFlag&&addressFlag){
                logger.info("执行了updateAddressee");
                return addressMapper.updateAddressee(addresseeId, addressee, addresseeTele, address);
            }else {
                logger.info("没执行updateAddressee");
                return 0;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<Address> queryAllAddressee(Integer userId) {
        return addressMapper.queryAllAddressee(userId);
    }

    @Override
    public Integer queryAddress(Integer userId) {
        return addressMapper.queryAddress(userId);
    }
}
